package com.elitesland.system.rest;

import com.elitesland.core.base.ApiCode;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.elitesland.system.entity.SysUdcDO;
import com.elitesland.system.param.SysUdcQueryParam;
import com.elitesland.system.service.SysUdcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.web.bind.annotation.*;

/**
 * <pre>
 * [功能说明]
 * </pre>
 *
 * @author Mir
 * @date 2020/6/25
 */
@RestController
@RequestMapping("/sys/codes")
@Api(value = "系统分类码维护", tags = {"系统分类码维护"})
@RequiredArgsConstructor
public class SysUdcController {

    private final SysUdcService sysUdcService;

    @PostMapping("/q")
    @ApiOperation("查询分类码")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", responseContainer = "List", response = SysUdcDO.class)
    )
    public ApiResult<PagingVO<SysUdcDO>> listCodes(@RequestBody SysUdcQueryParam param) {
        val ret = sysUdcService.search(param);
        return ApiResult.ok(ret);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据记录ID，获取对应的分类码记录")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = SysUdcDO.class)
    )
    public ApiResult<Object> getById(@PathVariable Long id) {
        val udcOpt = sysUdcService.findOneById(id);
        if (udcOpt.isPresent()) {
            return ApiResult.ok(udcOpt.get());
        }
        return ApiResult.fail(ApiCode.NOT_FOUND);
    }

    @GetMapping("/combo/{domain}/{udc}")
    @ApiOperation("通过领域码和分类码，获取下拉选择用的分类码和描述")
    public ApiResult<Object> listCombos(@PathVariable String domain, @PathVariable String udc) {
        val combos = sysUdcService.listCodeCombos(domain, udc);
        return ApiResult.ok(combos);
    }

    @PostMapping
    @ApiOperation("创建分类码")
    public ApiResult<Object> createCode(@RequestBody SysUdcDO udc) {
        sysUdcService.createCode(udc);
        return ApiResult.ok();
    }

    @PutMapping
    @ApiOperation("更新分类码")
    public ApiResult<Object> updateCode(@RequestBody SysUdcDO udc) {
        sysUdcService.updateCode(udc);
        return ApiResult.ok();
    }
}
